<!--
*@Author: QWP
*@Description: 应急方案编辑
*@Date: 2024-07-08 17:04:38
-->
<template>
  <u-dialog v-bind="$attrs"
    destroyOnClose
    :confirmLoading="loading"
    @submit="submit"
    @close="dialogClose">
    <u-busi-card title=""
      :bordered="false"
      class="f-margin-t14">
      <u-form :elements="formEls"
        :model="form"
        :gutter="0"
        :loading="loading"
        ref="formRef">
      </u-form>
    </u-busi-card>
  </u-dialog>
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted, h, inject, watch } from 'vue' 
import { useRoute, useRouter } from 'vue-router'
import { _IFormEl, _IExtra } from '@/src/components/common/form/interface';
import { FormInstance, FormItemProps } from 'ant-design-vue';
import dayjs from 'dayjs';
import { isValue, messageC } from '@/util/common'
import { cloneDeep } from 'lodash'
import * as schemeApi from '@/service/emergency/scheme'

const props = defineProps<{
  type?: string
}>()

const route = useRoute()
const srorts = inject('srorts')

const emits = defineEmits(['success'])

const loading = ref<boolean>(false)
const formRef = ref<FormInstance>()
const form = ref({})
const formItemProps: FormItemProps & _IExtra = {
  labelWidth: '90px',
  colon: false
}
const formEls: Array<_IFormEl> = [
  { label: '标题', key: 'emergencyPlanName', col: { span: 24 }, element: 'input', 
    formItemProps: { ...formItemProps, required: true }, 
  },
  { label: '类型', key: 'emergencyPlanType', col: { span: 24,}, element: 'select', 
    formItemProps: { ...formItemProps, required: true }, 
    componentProps: { options: srorts.filter(i => isValue(i.value)) }
  },
  { label: '文件编号', key: 'emergencyPlanNo', col: { span: 24 }, element: 'input', 
    formItemProps: { ...formItemProps, required: true }, 
  },
  { label: '附件', key: 'appendixSaveVOList', col: { span: 24 }, element: 'file', 
    formItemProps: { ...formItemProps, required: true }, 
    componentProps: {
      fileParams: { biztype: '1', bucket: 'EMERGENCY_BIZ_TYPE' },
      maxCount: 1
    },
  },
]

// 提交保存
const submit = async ()  => {
  await formRef.value.form.validate();
  loading.value = true;
  try {
    let form1 = cloneDeep(form.value)
    form1.appendixSaveVOList = form1.appendixSaveVOList[0]
    const res = await schemeApi.add(form1)
    if(res.code == 0) {
      emits('success');
      dialogClose()
      messageC(res.msg, 'success')
    } else {
      messageC(res.msg, 'error')
    }
  } catch (error) {}
  loading.value = false;
}

// 弹窗关闭
const dialogClose = () => {
  formRef.value.form.resetFields();
  form.value = {}
}

watch(() => props.type, (nval, oval) => {
  form.value.emergencyPlanType = nval
}, { immediate: true })

</script>

<style lang='scss' >
</style>